package org.dync.giftlibrary.widget;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.Component;
import org.dync.giftlibrary.ResourceTable;
import org.dync.giftlibrary.util.GiftAnimationUtil;

/**
 * Created by KathLine on 2017/7/7.
 */

public class CustormAnim implements ICustormAnim {
    @Override
    public Animator startAnim(final GiftFrameLayout giftFrameLayout, Component rootView) {
        //礼物飞入
        AnimatorProperty flyFromLtoR2 = GiftAnimationUtil.createFlyFromLtoR(rootView, 1500, 0, 2000, null);
        flyFromLtoR2.setStateChangedListener(new Animator.StateChangedListener() {
            @Override
            public void onStart(Animator animator) {
                giftFrameLayout.initLayoutState();
            }

            @Override
            public void onStop(Animator animator) {
            }

            @Override
            public void onCancel(Animator animator) {
            }

            @Override
            public void onEnd(Animator animator) {
                giftFrameLayout.comboAnimation(true);
            }

            @Override
            public void onPause(Animator animator) {
            }

            @Override
            public void onResume(Animator animator) {
            }
        });
        flyFromLtoR2.start();
        return flyFromLtoR2;
    }

    @Override
    public Animator comboAnim(final GiftFrameLayout giftFrameLayout, Component rootView, boolean isFirst) {
        final StrokeTextView anim_num = (StrokeTextView) rootView.findComponentById(ResourceTable.Id_animation_num);
        if (isFirst) {
            anim_num.setVisibility(Component.VISIBLE);
            anim_num.setText("x " + giftFrameLayout.getCombo());
            giftFrameLayout.comboEndAnim();//这里一定要回调该方法，不然连击不会生效
        } else {
            //数量增加
            AnimatorValue scaleGiftNum = GiftAnimationUtil.scaleGiftNum(anim_num);
            scaleGiftNum.setStateChangedListener(new Animator.StateChangedListener() {
                @Override
                public void onStart(Animator animator) {
                }

                @Override
                public void onStop(Animator animator) {

                }

                @Override
                public void onCancel(Animator animator) {

                }

                @Override
                public void onEnd(Animator animator) {
                    giftFrameLayout.comboEndAnim();//这里一定要回调该方法，不然连击不会生效
                }

                @Override
                public void onPause(Animator animator) {

                }

                @Override
                public void onResume(Animator animator) {

                }
            });
            scaleGiftNum.start();
        }
        return null;
    }

    @Override
    public Animator endAnim(final GiftFrameLayout giftFrameLayout, Component rootView) {
        AnimatorProperty animatorProperty = new AnimatorProperty();
        animatorProperty.setTarget(rootView);
        animatorProperty.moveFromY(0);
        animatorProperty.moveToY(-100);
        animatorProperty.alphaFrom(1.0f);
        animatorProperty.alpha(0);
        animatorProperty.setDuration(3000);
        return animatorProperty;
    }
}
